home *** CD-ROM | disk | FTP | other *** search
- Path: news.primenet.com!jstern
- From: jstern@primenet.com (Josh Stern)
- Newsgroups: comp.lang.smalltalk,comp.object,comp.lang.c++,comp.lang.java
- Subject: Re: The Good, the Bad, the Ugly, and the Wicked ...
- Date: 30 Mar 1996 01:41:02 -0700
- Organization: Primenet Services for the Internet
- Sender: root@primenet.com
- Message-ID: <4jis2u$hp8@nnrp1.news.primenet.com>
- References: <31570B8E.5A12@vmark.com> <4jbk0m$jt9@news4.digex.net> <AD7EDB45966858C12@mac-sandvik.engr.sgi.com> <3159B3E5.5281@bsis.com>
- X-Posted-By: jstern@usr3.primenet.com
-
- Leonard Lehew <leonard.lehew@bsis.com> wrote:
-
- >> >C++ very much has dynamic binding; lookup virtual functions.
- >>
- >> It depends how you defined dynamic binding, could you send messages to
- >> arbitrary methods in C++ during runtime (or java, by the way)?
-
- >C++ implements dynamic binding through virtual methods. When a virtual
- >member function is invoked (i.e., when a message is sent to an object)
- >the actual run time object must be of the class the compiler "expects" or
- >it must be of a class derived from the "expected" class. This means
- >that the polymorphism only extends to classes which are part of the
- >same inheritance tree.
- >
- >Smalltalk by comparison does not impose this restriction.
-
- Actually C++ doesn't either, see below.
-
- >You can send
- >any message selector to any object at runtime. If the receiving object
- >has a corresponding method, it will be invoked, regardless of its
- >inheritance.
- >
- >Which is better depends on your perspective and needs. The strong type
- >checking in C++ detects certain errors before testing, and C++ apps
- >generally perform better. On the other hand, there are many circumstances
- >where I want polymorphic behavior, and inheritance is just not the most
- >natural, convenient, or elegant way to achieve it.
-
- It should be pointed out that templates provide C++ with
- a different, *non-dynamic method of polymorphism*. The fact
- that it is non-dynamic means that it is not polymorphic
- from the point of view of the back end of the compiler -
- and strict type checking is enforced - but templates are
- *polymorphic from the point of view of the programmer*.
- Templates allow programming to be done generically.
- I can write a function like this:
-
- template <class T1, class T2>
- T1 funct(T1 x, T2 y) { return x.methoda + y.methodb; };
-
- Then elsewhere in the code one can call funct
- like this:
- String s1,s2;
- String s3 = funct(s1,s2);
-
- or like this:
-
- int x1; float x2, x3;
- x3 = funct(x1, x1);
-
- In each case, a different version of funct
- is automatically inferred and created by the
- compiler. The compiler doesn't mind doing this
- so long as there is a 'methoda' for the type of
- its first argument, a 'methodb' for the type of
- its second argument, and an operator+() (which
- can be either a method of the first argument or
- a global function in the current context) that
- takes an ordered pair of the appropriate type
- and returns an argument of the first type.
-
- The syntax is a bit ugly, but this technique combines
- the main advantages of dynamic polymorphism and
- static type checking. It is essentially static
- polymorphism, and it is polymorphism and not
- dynamic binding per se that
- is really what gives the programmer flexibility
- and leads to code reuse.
-
- Classes can also be templates, with parameterized
- types used to define any of their data, their
- methods, and/or their hierarchy.
-
- The only two unfortunate aspects of these techniques
- are a) that the return type of functions and methods
- cannot be used for inference about their
- parameterized types, though it may be parameterized
- as in the example above, and b) different executable
- code is generated for each different parameterization
- that is actually invoked, leading to great speed
- for small programs, but potentially large executables
- for large programs that are not careful about
- what all they are instantiating.
-
- - Josh
-
-
- --
- -------------------------------------------------------------------------------
- jstern
- jstern@primenet.com
- -------------------------------------------------------------------------------
-